package com.gewu.ordlelist.database.dbdao;

import android.content.Context;

import com.gewu.ordlelist.database.MyDatabase;
import com.gewu.ordlelist.database.dbbean.ByCar;
import com.gewu.ordlelist.utils.SqliteHelpr;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;

import java.sql.SQLException;
import java.util.List;

public class ByCarDao implements SqliteHelpr {

    private Dao<ByCar, Integer> byCars;

    public ByCarDao(Context context) {
        try {
            this.byCars = MyDatabase.getInstance(context).getDao(ByCar.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int add(Object bean) {
        int i = 0;
        try {
            i = byCars.create((ByCar) bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int delect() {
        try {
            byCars.delete(byCars.queryForAll());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delectFoId(Object id) {
        try {
            byCars.delete((ByCar) id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void inserts() {
        try {
            byCars.create(byCars.queryForAll());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Object bean) {
        try {
            byCars.update((ByCar) bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ByCar> quer() {
        List<ByCar> byCars1 = null;
        try {
            byCars1 = this.byCars.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return byCars1;
    }

    @Override
    public List<ByCar> querLike(String values) {
        try {
            QueryBuilder<ByCar, Integer> builder = byCars.queryBuilder();
            builder.where().eq("name", values);
            return builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }
}
